package marketplace.web.beans;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import marketplace.ejb.interfaces.IUsuarioEJB;
import marketplace.jpa.datas.DataRegistroAcceso;
import marketplace.web.utils.UtilsBean;

@ManagedBean
@SessionScoped
public class LoginBean
{

	@EJB
	IUsuarioEJB usuario;
	
	private int id;
	private String email;
	private String password;
	private static boolean logueado;
	
	
	@PostConstruct
	public void init(){
		this.email = "";
		this.password = "";
		LoginBean.logueado = false;
	}
	
	
	
	///////////////////////// GETERS Y SETERS ///////////////////////
	

	public int getId() { return id; }
	public void setId(int id) { this.id = id; }
	
	public String getEmail() { return email; }
	public void setEmail(String email) { this.email = email; }

	public String getPassword() { return password; }	
	public void setPassword(String password) { this.password = password; }

	public boolean isLogueado() { return logueado; }
	public static void setLogueado(boolean logueado) { LoginBean.logueado = logueado; }
	
	
	
	///////////////////////// OPERACIONES ////////////////////////////////////////
	
	
	//POST Login
	public void login(ActionEvent actionEvent) throws Exception{
		
		String pass;
		
		UtilsBean ub = new UtilsBean();
		pass = ub.cryptMD5(getPassword());
		//boolean existe = false;

		//Me Guardo el email del usuario logueado en la session
		HttpSession sesion = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);										
		
		if (usuario.existeAdministrador(getEmail(), pass)){
			sesion.setAttribute("mailUsuario", getEmail());
			sesion.setAttribute("tipoUsuario", "Administrador");
			//this.logueado = true;
			FacesContext.getCurrentInstance().getExternalContext().redirect("/MarketWEB/admin/index.xhtml");
		}else if (usuario.existeProveedor(getEmail(), pass)){
			sesion.setAttribute("mailUsuario", getEmail());
			sesion.setAttribute("tipoUsuario", "Proveedor");
			//this.logueado = true;
			FacesContext.getCurrentInstance().getExternalContext().redirect("/MarketWEB/proveedor/index.xhtml");
		}else if (usuario.existeCliente(getEmail())){
			if (usuario.validarLogin(getEmail(), pass)){
				sesion.setAttribute("mailUsuario", getEmail());
				sesion.setAttribute("tipoUsuario", "Cliente");
				LoginBean.logueado = true;
				DataRegistroAcceso dr = new DataRegistroAcceso();
				dr.setCliente(getEmail());
				
				SimpleDateFormat f =new SimpleDateFormat("dd/MM/yy");
				Date hoy = new Date();		
		 		String formattedDate = f.format(hoy);
		 		dr.setFechaAcceso(formattedDate);		 		
				usuario.registrarAcceso(dr);
				
				FacesContext.getCurrentInstance().getExternalContext().redirect("/MarketWEB/market/index.xhtml");
			}else{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"ERROR: Compruebe su contrasena",""));	
			}
		}else{
			sesion.setAttribute("mailUsuario", null);
			sesion.setAttribute("tipoUsuario", null);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"ERROR: Compruebe su email o contrasena",""));
		}
	}
		
	//POST Logout
	public void logout() throws Exception {
				
		this.email = "";
		this.password = "";
		LoginBean.logueado = false;
		
		HttpSession sesion = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		
		sesion.setAttribute("mailUsuario", null);
		sesion.setAttribute("tipoUsuario", getEmail());
		
		FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
		FacesContext.getCurrentInstance().getExternalContext().redirect("/MarketWEB/market/index.xhtml");
	}
	
	//Redireccion al Index
	public void index() throws Exception{
		FacesContext.getCurrentInstance().getExternalContext().redirect("/MarketWEB/market/index.xhtml");
	}


	
}
